/**
 * Created by TiuFam on 11/26/2014.
 */

var SettingScene = cc.Layer.extend({

    ctor: function () {
        this._super();
        daifugo.settingscene = this;
        this.init();

    },
    init: function(){
        daifugo.levelCPU = [0, 0, 0];
        var me = this;
        var size = cc.Director.getInstance().getWinSize();
        var mSprTop = cc.Sprite.create(g_imageRes.s_top);
        mSprTop.setPosition(size.width / 2, size.height / 2);
        this.addChild(mSprTop);


        var SettingTitle = cc.LabelTTF.create("SETTING", "Time New Roman", 60);
        SettingTitle.setPosition(size.width * 0.348, size.height * 0.55);
        this.addChild(SettingTitle);

        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);
        var title = cc.LabelTTF.create("CPU 0", "Arial", 31);
        title.setPosition(size.width*(290/960),size.height*(311/720));
        this.addChild(title);

        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);
        var item = cc.MenuItemToggle.create(
            cc.MenuItemFont.create("Easy"),
            cc.MenuItemFont.create("Normal"),
            cc.MenuItemFont.create("Hard"));

        var title1 = cc.LabelTTF.create("CPU 2", "Arial", 31);
        title1.setPosition(size.width*(290/960), size.height*(229/720));
        this.addChild(title1);
        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);
        var item1 = cc.MenuItemToggle.create(
            cc.MenuItemFont.create("Easy"),
            cc.MenuItemFont.create("Normal"),
            cc.MenuItemFont.create("Hard"));

        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);
        var title2 = cc.LabelTTF.create("CPU 1", "Arial", 31);
        title2.setPosition(size.width*(290/960),size.height*(270/720));
        this.addChild(title2);

        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);
        var item2 = cc.MenuItemToggle.create(
            cc.MenuItemFont.create("Easy"),
            cc.MenuItemFont.create("Normal"),
            cc.MenuItemFont.create("Hard"));

        cc.MenuItemFont.setFontName("Charcoal");
        cc.MenuItemFont.setFontSize(32);

        var menu = cc.Menu.create(item,item1, item2);
        menu.alignItemsInColumns(1, 1, 1);
        menu.setPosition(size.width*(400/960),size.height*(270/720));
        this.addChild(menu);

        var mSprButtonPlayGame = cc.Sprite.create(g_imageRes.s_top_btn_play);
        var mBtnPlayGame = cc.ControlButton.create(mSprButtonPlayGame);
        mBtnPlayGame.setAdjustBackgroundImage(true);
        mBtnPlayGame.setPosition(size.width * 0.348, size.height * 0.20);
        mBtnPlayGame.addTargetWithActionForControlEvents(this, function () {
            var nextScene = cc.Scene.create();
            var nextLayer = new PlayLayer;
            nextScene.addChild(nextLayer);
            daifugo.serverHandler.contactServer('message', function () {
            },{action: "LEVEL_CPU", dataLevelCPU: daifugo.levelCPU });

            cc.Director.getInstance().replaceScene(nextScene);
        }, cc.CONTROL_EVENT_TOUCH_DOWN);
        mBtnPlayGame.setScale(0.4);
        this.addChild(mBtnPlayGame);

        var PlayContinue = cc.LabelTTF.create("Click PLAY to continue game", "Arial", 20);
        PlayContinue.setPosition(size.width * 0.348, size.height * 0.10);
        this.addChild(PlayContinue);

        var lblChangeLevel = cc.LabelTTF.create("( Click to change computers level )", "Arial", 15)
        lblChangeLevel.setPosition(size.width * 0.348, size.height * 0.50);
        this.addChild(lblChangeLevel);

        // Declare the callback for the table setting
        var arrItem = [item, item1, item2];
        for(var i = 0; i < arrItem.length; i++){
            arrItem[i].index = i;
            arrItem[i].setCallback(function(item){
                daifugo.levelCPU[item.index] = (daifugo.levelCPU[item.index] + 1) % NB_CPU;
                console.log(daifugo.levelCPU);
            });
        }
    }
});